package io.adrainty.algorithm.mars;

/**
 * 环形数组中的最大贡献值
 * @link <a href="https://www.marscode.cn/practice/364l85wv6oo6lq?problem_id=7424418560931561516">...</a>
 * @author AdRainty
 * @version V1.0.0
 * @since 2024/11/30 下午5:18
 */
public class Solution021 {

    private Solution021() {}

    public static int solution(int n, int[] a) {
        // write code here
        int maxContribution = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                maxContribution = Math.max(maxContribution, minDistance(n, i, j) * (a[i] + a[j]));
            }
        }
        return maxContribution; // Placeholder return
    }

    public static int minDistance(int n, int i, int j) {
        int distanceClockwise = (j - i + n) % n;
        int distanceCounterclockwise = (i - j + n) % n;
        return Math.min(distanceClockwise, distanceCounterclockwise);
    }

    public static void main(String[] args) {
        System.out.println(solution(3, new int[]{1, 2, 3}) == 5);
        System.out.println(solution(4, new int[]{4, 1, 2, 3}) == 12);
        System.out.println(solution(5, new int[]{1, 5, 3, 7, 2}) == 24);
    }

}
